/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clip.net;

import L.L;
import clip.clipApp;
import clip.model.Options;
import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author admin
 */
public class RMIServer {
    
    private clipApp app;
    private Options options;
    private Registry registry;

    public clipApp getApp() {
        return app;
    }
    
    public RMIServer(clipApp ca) {
        app=ca;
        options=app.options;
        try {
            startRMIService();
        } catch (RemoteException ex) {
            L.w("RMIServer initialisiert aber startRMIService hat fehler produziert:"+ex.getMessage());
            JOptionPane.showMessageDialog(null, ex.getMessage(), "rmiserver error", JOptionPane.ERROR_MESSAGE);
        }
        L.d("rmiserver started on port "+options.MY_SERVER_PORT+" || Registry"+registry.toString());
    }
    
    //static getter, weiss net obs geht, glaub schon
    public Registry getRegistry() {
        if (registry == null) {
            try {
                startRMIService();
            } catch (RemoteException ex) {
                Logger.getLogger(RMIServer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return registry;
    }

    public boolean isRunning() {
        try {
            return registry != null && registry.list().length > 0;
        } catch (RemoteException ex) {
            Logger.getLogger(RMIServer.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            return false;
        }
    }

    public void startRMIService() throws RemoteException {
        // create in server registry
        registry = java.rmi.registry.LocateRegistry.createRegistry(Options.getInstance().MY_SERVER_PORT);
        registerObjects();
    }

    public void stopRMIService() {
        try {
            registry.unbind("RMIApi");
        } catch (RemoteException ex) {
            Logger.getLogger(RMIServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotBoundException ex) {
            Logger.getLogger(RMIServer.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }

    private void registerObject(String name, Remote remoteObj)
            throws RemoteException, AlreadyBoundException {
        registry.bind(name, remoteObj);
        L.d("Registered: " + name + " -> " + remoteObj.getClass().getName() + "[" + remoteObj + "]");
    }

    private void registerObjects() {
        try {
            registerObject("RMIApi", new RMIApiImpl(this));
        } catch (RemoteException ex) {
            Logger.getLogger(RMIServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AlreadyBoundException ex) {
            Logger.getLogger(RMIServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

